<?php
namespace app\jobmanagement\controller;

use app\jobmanagement\service\Common as CommonService;
use app\jobmanagement\service\Task as TaskService;
use app\jobmanagement\service\Settle as SettleService;
use app\jobmanagement\service\TransferConfig as TransferConfigService;
use app\jobmanagement\service\User as UserService;

class User extends Base
{
    protected $sTask;

    protected $sCommon;

    protected $sSettle;

    protected $sUser;

    public function __construct()
    {
        parent::__construct();
        $this->sTask = new TaskService();
        $this->sCommon = new CommonService();
        $this->sSettle = new SettleService();
        $this->sUser = new UserService();
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 15:21
     * @description 获取用户资金
     * @return \think\Collection|\think\model\Collection
     */
    public function workerCapital()
    {
        $res = $this->sUser->getWorkerCapital();
        if (empty($res)) {
            return $this->apiError("请求失败,请重试");
        } else {
            return $this->apiSuccess('success', $res);
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 15:41
     * @description 获取用户资金记录
     * @return \think\Collection|\think\model\Collection
     */
    public function workerCapitalLog()
    {
        $param['page'] = input('page', 1);
        $param['year'] = input('year');
        $param['month'] = input('month');
        $res = $this->sUser->getWorkerCapitalLog($param);
        return $this->apiSuccess('success', $res);
    }

    /**
     * @auther zlq
     * @create_time 2020/6/8 11:38
     * @description 获取用户月度资金统计
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWorkerCapitalMonthCensus()
    {
        $param['page'] = input('page', 1);
        $res = $this->sUser->getWorkerCapitalMonthCensus($param);
        return $this->apiSuccess('success', $res);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 17:25
     * @description 用户申请提现接口
     * @return \think\Collection|\think\model\Collection
     */
    public function workWithdraw()
    {
        $amount = input('amount', 0);
        $remark = input('remark');
        if (!is_numeric($amount) || $amount < 0) {
            return $this->apiError("金额不正确,请重试");
        }
        $amount = (float)$amount;
        // 数据格式是否正确
        $temp = explode ( '.', $amount);
        if (sizeof ( $temp ) > 1) {
            $decimal = end ( $temp );
            if (strlen($decimal) > 2) {
                return $this->apiError("提现金额最多有两位小数, 请重试");
            }
        }
        $obj = new TransferConfigService();
        $config = $obj->getWithdrawConfig();
        $res = $this->sUser->getWorkWithdraw($amount, $remark);
        switch ($res) {
            case 1:
                return $this->apiSuccess('申请提现审核已提交成功');
                break;
            case -1:
                return $this->apiError('参数不正确,请重试');
                break;
            case -2:
                return $this->apiError('用户信息信息不正确,请重试');
                break;
            case -3:
                return $this->apiError('账户余额不足,请重试');
                break;
            case -4:
                return $this->apiError('用户银行账号信息不正确,请先核实');
                break;
            case -5:
                return $this->apiError('今日提现次数已达上限,请明日重试');
                break;
            case -6:
                return $this->apiError('今日提现总额已达上限5000,请明日重试');
                break;
            case -7:
                return $this->apiError('用户账号金额不足提现标准: '.$config['guaranteed_cash_withdrawal'].',请重试');
                break;
            case -8:
                return $this->apiError('单笔提现金额不得超出: '.$config['maximum_single_cash_withdrawal'].',请明日重试');
                break;
            default:
                return $this->apiError('申请提现审核提交失败,请重试');
                break;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 21:35
     * @description 获取组长列表
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function leaderList()
    {
        $user_level = session('user_level');
        if ($user_level != 1) {
            return $this->apiError('您没有权限访问该页面');
        }
        $param['page'] = input('page');
        $list = $this->sUser->getleaderList($param);
        return $this->apiSuccess('success', $list);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 21:43
     * @description 移除领导
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function removeLeader()
    {
        $user_id = input('user_id', 0);
        if (!$user_id) {
            return $this->apiError('参数不正确,请重试');
        }
        $res = $this->sUser->removeLeader($user_id);
        switch ($res) {
            case 1:
                return $this->apiSuccess('移除组长权限成功');
                break;
            case -1:
                return $this->apiError('参数不正确,请重试');
                break;
            case -2:
                return $this->apiError('用户信息不正确,请重试');
                break;
            case -3:
                return $this->apiError('用户不是组长,请刷新页面');
                break;
            case -4:
                return $this->apiError('该组长不属于本厂区,请刷新页面');
                break;
            default:
                return $this->apiError('移除组长权限失败,请重试');
                break;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 21:58
     * @description 添加组长
     * @return \think\Collection|\think\model\Collection
     */
    public function addLeader()
    {
        $user_id = input('user_id', "");
        $user_array = explode(',', trim($user_id, ','));
        $user_array = array_filter($user_array);
        try {
            $res = $this->sUser->addLeader($user_array);
        }catch (\Exception $exception) {
            addLog($exception->getMessage());
        }
        switch ($res) {
            case 1:
                return $this->apiSuccess('添加组长成功');
                break;
            case -1:
                return $this->apiError('参数不正确,请重试');
                break;
            case -2:
                return $this->apiError('用户信息获取失败,请重试');
                break;
            case -3:
                return $this->apiError('数据不正确,请重试');
                break;
            case -4:
                return $this->apiError('无法将管理员设置为组长,请重试');
                break;
            default:
                return $this->apiError('添加组长失败,请重试');
                break;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 22:07
     * @description 修改用户银行账号
     */
    public function editUserAccountCode()
    {
        $user_id = input('user_id', '');
        if (!$user_id) {
            return $this->apiError('参数不正确,请重试');
        }
        $account_code = input('account_code','');
        $account_user = input('account_user','');
        $account_user = trim($account_user);
        $account_code = trim($account_code);
        if (empty($account_code)) {
            return $this->apiError('参数不正确,请重试');
        }
        if (empty($account_user)) {
            return $this->apiError('参数不正确,请重试');
        }
        if (!$account_code) {
            return $this->apiError('支付宝账号不正确,请重试');
        }
        $res = $this->sUser->editUserAccountCode($user_id, $account_code, $account_user);
        switch ($res) {
            case 1:
                return $this->apiSuccess('用户支付宝账号修改成功');
                break;
            case -1:
                return $this->apiError('参数不正确,请重试');
                break;
            case -2:
                return $this->apiError('用户信息不正确,请重试');
                break;
            case -3:
                return $this->apiError('您尚未对支付宝名称和支付宝账号做出修改,请重试');
                break;
            default:
                return $this->apiError('用户支付宝账号修改失败,请重试');
                break;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/23 9:43
     * @description 不可选取部门
     * @return \think\Collection|\think\model\Collection
     */
    public function notCheck()
    {
        $res1 = $this->sUser->getNotCheckDeptList();
        $res2 = $this->sUser->getNotCheckUserList();
        return $this->apiSuccess('success', ['dept' => $res1, 'user' => $res2]);
    }
}